package com.syeet.gulimall.search.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 用来判断是windows系统，才能给容器中注册组件
 * 使用@Conditional({WindowsCondition.class})
 *
 * @author xhl
 * @date 2023/5/17
 */
public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        //获取ioc的beanFactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //获取bean注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        //给容器中注册一个searchParam bean
//        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(SearchParam.class);
//        registry.registerBeanDefinition("searchParam",rootBeanDefinition);
        //获取ioc环境信息
        Environment environment = context.getEnvironment();
        //获取到当前的操作系统类型
        String property = environment.getProperty("os.name");
        if (property.contains("Windows")) {
            return true;
        }
        return false;
    }
}
